// pages/chooseSchool/chooseSchool.js
var app=getApp();
const http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabId:0,
    schoolList:[],
    keywords:''
  },


  getLocation() {
    wx.showLoading({
      title: '加载中...',
    })
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        const latitude = res.latitude
        const longitude = res.longitude
       
        // wgs84转百度坐标系
        // var ssws = this.wgs84togcj02(longitude, latitude);
        // ssws = this.gcj02tobd09(ssws[0], ssws[1]);
        // //解决定位偏移
        // var ssssss1 = ssws[1] - 0.000160
        // var ssssss2 = ssws[0] - 0.000160
        // console.log('latitude:', ssssss1.toFixed(6), 'longitude:', ssssss2.toFixed(6));
        // this.setData({
        //   latitude: ssssss1.toFixed(6),
        //   longitude: ssssss2.toFixed(6)
        // })
        this.setData({
          latitude,
          longitude
        })
        this.getShoolList(1);
      },
      fail: () => {
        http.showToast('定位失败!' + '', '', 'none')
      }
    })
  },

//获取定位学校
getShoolList(type){
  var uid = app.globalData.uid;
  var serach = '%' + this.data.keywords + '%'
  console.log(uid, type,serach,this.data.longitude, this.data.latitude)
 
 
  http.fetchApi('procedure_t', { p: `R2003005|${uid}|${type}|${serach}|${this.data.longitude}|${this.data.latitude}`}).then(res=>{
    if(res.data.code==1){
      wx.hideLoading()
      var schoolList = res.data.results;
      console.log(schoolList)
      this.setData({
        schoolList
      })
    }else{
      http.showToast('加载失败!' + '', '', 'none')
    }
  })
},

//学校搜索
  searchShcool(e){
    console.log(e.detail.value);
    this.setData({
      keywords: e.detail.value
    })
    if (this.data.tabId == 0) {
      this.getShoolList(1)
    } else {
      this.getShoolList(2)
    }
  },
  //切换Tab
  toggleTab(e) {
  
    var tabId = e.currentTarget.dataset.id;
    this.setData({
      tabId
    })
    if(tabId==0){
      this.getShoolList(1)
    }else{
      this.getShoolList(2)
    }
  },

  //选择校区
  selectSchool(e){
    var oldSchool = wx.getStorageSync('nowSchool');
    var nowSchool = e.currentTarget.dataset.item;
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2003007|${uid}|${nowSchool.id}` })
    
    wx.setStorageSync('nowSchool', nowSchool);
    console.log(oldSchool, nowSchool)
    if (oldSchool.id != nowSchool.id){
      app.globalData.isCheckSchool=1;
      app.globalData.isAddCheckSchool = 1;
    }
    // var pages = getCurrentPages();
    // var prePage = pages[pages.length - 2];
    // if (prePage.route == 'pages/FlashDelivery/FlashDelivery'){
    //   prePage.getGoodsList(1,0);
    // }
   
    if (wx.getStorageSync('shopCar') || wx.getStorageSync('shopCar1')){ 
      if (oldSchool.id != nowSchool.id){
        wx.showModal({
          title: '提示',
          content: '你已切换新的学校，请清空购物车其他校区商品',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.removeStorageSync('shopCar');
              wx.removeStorageSync('shopCar1');

              wx.removeStorageSync('totalPrice');
              wx.removeStorageSync('jiadanId');
              wx.removeStorageSync('hongbao');
              wx.navigateBack({

              })
            }
          }
        })
      }else{
        wx.navigateBack({

        })
      }
     
    }else{
      wx.navigateBack({

      })
    }
    
    
     
   
  },
  /**
* WGS84转GCj02
* @param lng
* @param lat
* @returns {*[]}
*/

  wgs84togcj02: function (lng, lat) {
    var that = this

    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var PI = 3.1415926535897932384626;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;
    if (that.out_of_china(lng, lat)) {
      return [lng, lat]
    }
    else {
      var dlat = that.transformlat(lng - 105.0, lat - 35.0);
      var dlng = that.transformlng(lng - 105.0, lat - 35.0);
      var radlat = lat / 180.0 * PI;
      var magic = Math.sin(radlat);
      magic = 1 - ee * magic * magic;
      var sqrtmagic = Math.sqrt(magic);
      dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
      dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
      var mglat = lat + dlat;
      var mglng = lng + dlng;
      return [mglng, mglat]
    }
  },
  /**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
* 即谷歌、高德 转 百度
* @param lng
* @param lat
* @returns {*[]}
*/
  gcj02tobd09: function (lng, lat) {
    var that = this
    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var PI = 3.1415926535897932384626;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;
    var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
    var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
    var bd_lng = z * Math.cos(theta) + 0.0065;
    var bd_lat = z * Math.sin(theta) + 0.006;
    return [bd_lng, bd_lat]
  },
  transformlat: function (lng, lat) {
    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var PI = 3.1415926535897932384626;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;
    var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
    return ret
  },

  transformlng: function (lng, lat) {
    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var PI = 3.1415926535897932384626;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;
    var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
    return ret
  },

  /**
   * 判断是否在国内，不在国内则不做偏移
   * @param lng
   * @param lat
   * @returns {boolean}
   */
  out_of_china: function (lng, lat) {
    return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})